<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class City extends Common
{

	//列表
    public function index()
    {
        $title = $this->request->get('title');
        $provice = $this->request->get('provice');
        $this->assign(array('title'=>$title));
        $this->assign(array('provice'=>$provice));
        $where = [];
        if($title)
            $where['title'] = array('like','%'.$title.'%');
        if($provice)
            $where['provice'] = array('exp','in(select id from cg_provice where title like "%'.$provice.'%")');
        $count = Db::table('cg_city')->where($where)->count();
        $data = Db::table('cg_city')->order("id desc")->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('employee')->where(array('id'=>$v['cuser']))->value('em_name');
            $data[$k]['pusername'] = Db::table('employee')->where(array('id'=>$v['puser']))->value('em_name');
            $data[$k]['provice'] = Db::table('cg_provice')->where(array('id'=>$v['provice']))->value('title');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
        return $this->fetch('index');
    }
    public function add(){

        $validate = new Validate([
            'title'  => 'require|max:100|unique:cg_city',
            'provice'  => 'require'
        ]);
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['provice'] = $this->request->post('provice');
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_city')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }

    }

    public function addview(){
        return $this->fetch();
    }
    public function updateview(){
        $id = $this->request->get('id');
        $data = Db::table('cg_city')->where(array('id'=>$id))->find();
        $data['provice_name'] = Db::table('cg_provice')->where(array('id'=>$data['provice']))->value('title');
        $this->assign('data',$data);
        return $this->fetch();
    }

    //查询所有省份
    public function selectprovice(){
        $data = Db::table('cg_provice')->select();
        foreach($data as $k=>$v){
            $temp = Db::table('cg_city')->where(array('id'=>$v['id']))->select();
            if($temp)
                $data[$k]['child'] = $temp;
        }
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function provicejson(){
        //查询所有省份
        $data = Db::table('cg_provice')->field('title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    public function update(){

        $id = $this->request->post('id');
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['id'] = $id;
        $data['provice'] = $this->request->post('provice');
        $validate = new Validate([
            'title'  => 'require|max:100|unique:cg_city',
            'id' => 'require|number',
            'provice' => 'require|number'
        ]);

        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_city')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"修改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"修改失败"
            ]);die;
        }

    }

    public function delete(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_city')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }
}
